package net.yla.smartbus.core.constants;

import net.yla.smartbus.core.exception.EnumNotFoundException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: ZhouChengLei
 * @Date: 2019/7/18 11:22
 * @Version: 1.0
 */
public enum ModifyFlagEnum {

    /**
     * 修改标记：正常
     */
    NORMAL(0,"正常"),
    /**
     * 修改标记：新增
     */
    ADD(1,"新增"),

    /**
     * 修改标记：修改
     */
    UPDATE(2,"修改"),

    /**
     * 修改标记：删除/作废
     */
    DELETE(3,"删除");
    private int code;
    private String desc;
    ModifyFlagEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    private static final Map<Integer, ModifyFlagEnum> BY_VALUE_MAP = new LinkedHashMap<>();

    static {
        for (ModifyFlagEnum modifyFlagEnum : ModifyFlagEnum.values()) {
            BY_VALUE_MAP.put(modifyFlagEnum.getCode(), modifyFlagEnum);
        }
    }

    /**
     * 根据code查询对应的处理结果类型枚举
     */
    public static ModifyFlagEnum convertCode2Enum(int code) throws EnumNotFoundException {
        if (!BY_VALUE_MAP.containsKey(code)) {
            throw new EnumNotFoundException("枚举对应的code" + code + "不存在");
        }
        return BY_VALUE_MAP.get(code);
    }

    /**
     * 根据枚举name查询对应的处理结果类型枚举
     */
    public static ModifyFlagEnum convertStr2Enum(String str) throws EnumNotFoundException {
        try {
            return ModifyFlagEnum.valueOf(str);
        } catch (Exception e) {
            throw new EnumNotFoundException("枚举对应常量" + str + "不存在");
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return this.name();
    }
    
}
